package com.onlinevideo.service.impl;

import com.onlinevideo.dao.CommentDao;
import com.onlinevideo.dao.impl.CommentDaoImpl;
import com.onlinevideo.entity.Comment;
import com.onlinevideo.entity.User;
import com.onlinevideo.service.CommentService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CommentServiceImpl implements CommentService {

    private CommentDao commentDao = new CommentDaoImpl();

    @Override
    public int insert(Comment comment) {
        return commentDao.insert(comment);
    }

    @Override
    public int delete(Long id) {
        commentDao.deleteByFid(id);
        return commentDao.delete(id);
    }

    @Override
    public int update(Comment comment) {
        return commentDao.update(comment);
    }

    @Override
    public Comment queryById(Long id) {
        return commentDao.queryById(id);
    }

    @Override
    public List<Comment> queryAll() {
        return commentDao.queryAll();
    }

    @Override
    public List<Comment> queryByVideoId(Long id) {
        return commentDao.queryByVideoId(id);
    }


    @Override
    public Comment queryByCommonId(Long id) {
        return commentDao.queryByCommentId(id);
    }

    @Override
    public List<List<Comment>> queryForListByVideoId(Long videoId) {
        List<List<Comment>> lists = new ArrayList<>();
        List<Comment> comments = commentDao.queryByVideoIdS1(videoId);
        for (Comment comment : comments) {
            List<Comment> commentList = new ArrayList<>();
            commentList.add(comment);
            commentList.addAll(commentDao.queryByFatherIdS1(comment.getCommentId()));
            lists.add(commentList);
        }
        return lists;
    }

    @Override
    public List<Comment> queryByFatherIdS0(Long fatherId) {
        return commentDao.queryByFatherIdS0(fatherId);
    }

    @Override
    public List<Comment> queryByFatherIdS1(Long fatherId) {
        return commentDao.queryByFatherIdS1(fatherId);
    }

    @Override
    public List<Comment> queryByVideoIdS0(Long id) {
        return commentDao.queryByVideoIdS0(id);
    }

    @Override
    public List<Comment> queryByVideoIdS1(Long id) {
        return commentDao.queryByVideoIdS1(id);
    }

    @Override
    public Object[] addComment(Long fid, Long vid, User user,String content) {
        long id = Math.abs(UUID.randomUUID().getLeastSignificantBits());
        Comment comment = new Comment(null, id, fid, 1, user.getId(), vid, content, String.valueOf(System.currentTimeMillis()));
        Object[] data = new Object[6];
        data[0]=user.getLogoUrl();
        data[1]=user.getNickname();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(comment.getCommentTime()));
        data[2]= format;
        data[3]=String.valueOf(id);
        data[4]=comment.getCommentContent();
        int insert = commentDao.insert(comment);
        data[5] = insert;
        return data;
    }

    @Override
    public List<Comment> queryAllS0() {
        return commentDao.queryAllS0();
    }

    @Override
    public List<Comment> queryAllS1() {
        return commentDao.queryAllS1();
    }

}
